package com.apchernykh.types;

import java.sql.Date;
import java.util.Locale;
import java.util.TimeZone;

import org.w3c.dom.Node;

import android.os.Parcel;
import android.os.Parcelable;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import com.apchernykh.rozhlasapp.utils.ConditionalLog;
import com.apchernykh.rozhlasapp.utils.XMLParsingHelper;

public class RadioProgram implements Parcelable {
	final static long mOneDayMillisConst = 1000 * 60 * 60 * 24;
	final static SimpleDateFormat mLocalDateParser = createLocalDateParser();
	static DateFormat mLocalShortDateFormat = createDateFormatter();

	public String mName;
	public String mDescription;
	public String mType;
	public long mStartTime;
	public long mStopTime;

    private static SimpleDateFormat createLocalDateParser()
    {
    	SimpleDateFormat aSdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.US);
    	aSdf.setTimeZone(TimeZone.getTimeZone("Europe/Prague"));
    	return aSdf;
    }
    
	public static final Parcelable.Creator<RadioProgram> CREATOR = new Parcelable.Creator<RadioProgram>() {
		public RadioProgram createFromParcel(Parcel iParcel) {
			return new RadioProgram(iParcel);
		}

		public RadioProgram[] newArray(int iSize) {
			return new RadioProgram[iSize];
		}
	};

	public RadioProgram(Node iRootNode) {
		mName = XMLParsingHelper.extractChildText(iRootNode, "nazev");
		mDescription = XMLParsingHelper.extractChildText(iRootNode, "popis");
		mType = XMLParsingHelper.extractChildText(iRootNode, "typporadu_nazev");
		mStartTime = parseStartTime(iRootNode, "casvysilani");

		XMLParsingHelper.TimedNamedEvent aCasVysilani = XMLParsingHelper
				.extractTimedNamedEvent(iRootNode, "casvysilani");
	    long aDuration = aCasVysilani.mUntil * 1000 - aCasVysilani.mFrom * 1000;
		if (aDuration < 0) {
			aDuration += mOneDayMillisConst;
		}
		mStopTime = mStartTime+aDuration;
		
		// When there is a description - we don't care what is the show type,
		// When there is no description - let the show type to be the
		// description
		if (mDescription == null || mDescription.length() == 0) {
			mDescription = mType;
		}

	}

	private long parseStartTime(Node iRootNode, String iChildElementName) {
		final String aTimeTermin = XMLParsingHelper.extractChildText(iRootNode, iChildElementName);
		long aStartTimeLong = 0;
	    try {
			final java.util.Date aStartDate = mLocalDateParser.parse(aTimeTermin);
			aStartTimeLong = aStartDate.getTime();
		} catch (ParseException e) {
			//This is not a critical error, so don't crash here. Just add an annoying 
			e.printStackTrace();
			ConditionalLog.e(this.toString(),
					"Unable to parse the date. "+aTimeTermin+" Probably, XML format is changed in CR, there is a need for a program update.", e);
			aStartTimeLong = System.currentTimeMillis();
		}
	    return aStartTimeLong;
	}

	public RadioProgram(Parcel iParcel) {
		mName = iParcel.readString();
		mDescription = iParcel.readString();
		mType = iParcel.readString();
		mStartTime = iParcel.readLong();
		mStopTime = iParcel.readLong();
	}

	@Override
	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void writeToParcel(Parcel oDestinationParcel, int iFlagsReserved) {
		oDestinationParcel.writeString(mName);
		oDestinationParcel.writeString(mDescription);
		oDestinationParcel.writeString(mType);
		oDestinationParcel.writeLong(mStartTime);
		oDestinationParcel.writeLong(mStopTime);
	}

	@Override
	public String toString() {
		return super.toString() + "(" + mName + ":" + mDescription + ":"
				+ mType + ":" + formatTimeInterval() + ")";
	}

	public static DateFormat createDateFormatter() {
		DateFormat aFormat = DateFormat.getTimeInstance(DateFormat.SHORT,
				Locale.FRANCE);
		aFormat.setTimeZone(TimeZone.getTimeZone("Europe/Prague"));
		return aFormat;
	}

	public String formatTimeInterval() {
		return mLocalShortDateFormat.format(new Date(mStartTime)) + "-"
				+ mLocalShortDateFormat.format(new Date(mStopTime));
	}

}
